using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
    public class Phieu_Deal_DAL
    {
        private SqlConnection cn = null;
        public Phieu_Deal_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Strconnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(Phieu_Deal_DTO Phieu_Deal)
        {
            SqlCommand cm = new SqlCommand("Phieu_Deal_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDCTHoaDon", Phieu_Deal.MaCTHD);
            cm.Parameters.AddWithValue("@SeRi", Phieu_Deal.SeRi);
            cm.Parameters.AddWithValue("@MaTinhTrang_Phieu", Phieu_Deal.MaTinhTrang_Phieu);
            cm.ExecuteNonQuery();
        }
        public void Update(Phieu_Deal_DTO Phieu_Deal)
        {
            SqlCommand cm = new SqlCommand("Phieu_Deal_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", Phieu_Deal.ID);
            cm.Parameters.AddWithValue("@IDCTHoaDon", Phieu_Deal.MaCTHD);
            cm.Parameters.AddWithValue("@SeRi", Phieu_Deal.SeRi);
            cm.Parameters.AddWithValue("@MaTinhTrang_Phieu", Phieu_Deal.MaTinhTrang_Phieu);

            cm.ExecuteNonQuery();
        }
        public void Delete(string iD)
        {
            SqlCommand cm = new SqlCommand("Phieu_Deal_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", iD);
            cm.ExecuteNonQuery();
        }
        public PhieuDealCollection Get(string where, int PageIndex, int PageSize,
            string OrderBy, string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("Phieu_Deal_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize",PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            cm.Parameters.Add(new SqlParameter("@TOTALRECORDS", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            PhieuDealCollection PhieuDealCollection = new PhieuDealCollection();

            while (dr.Read())
            {
                Phieu_Deal_DTO PhieuDeal = new Phieu_Deal_DTO();

                PhieuDeal.ID = dr.GetValue(0).ToString();
                PhieuDeal.MaCTHD = int.Parse(dr.GetValue(1).ToString());
                PhieuDeal.SeRi = dr.GetValue(2).ToString();
                PhieuDeal.MaTinhTrang_Phieu = int.Parse(dr.GetValue(3).ToString());

                PhieuDealCollection.Add(PhieuDeal);

            }
            dr.Close();
            TotalRecords = int.Parse(cm.Parameters["@TOTALRECORDS"].Value.ToString());
            return PhieuDealCollection;

        }


       
    }
}
